<?php
class calc extends command {

	public function __construct(){
		$this->name = 'calc';
		$this->public = true;
	}

	public function help(){
		return "Uso: !calc <expresión matemática a evaluar> . Devuelve el valor numérico de dicha expresión matemática.";
	}	
	
	public function process($args){
		$this->output = "";
		$originalargs = $args;
		$args = trim($args);
		$args = str_replace("+", "%2B", $args);		
		$args = str_replace(" ", "+",$args);
		
		$buffer="";
		$gestor = @fopen("http://www.google.com.mx/search?hl=es&q=" . $args . "&btnG=Buscar+con+Google&meta=&aq=f&oq=", "r");
		while (!feof($gestor)) {
			$buffer = $buffer.fgets($gestor, 4096);
		}
		fclose ($gestor);

		$buffer = str_replace("\n", "", $buffer);
		
		preg_match ("/<h2 class=r style=\"font-size:138%\">(.*?)<\/h2>/", $buffer, $matches);
		$results = $matches[1];
		
		$results = strip_tags($results);
		$results = trim($results);
		if ( strlen($results)){
			$this->output = strip_tags($matches[1]);
		} else {
			$this->output = "Lo siento. No pude resolver < {$originalargs} >.";
		}
	}
}
?>